<?php
$basedir = dirname(__FILE__);
class Image 
{
	//image source identifier
	public $im;
	
	//image height
	public $height;
	
	//image width
	public $width;
	
	//image error
	public $error;
	
	//construct function
	//param :null
	//return :null
	function __construct()
	{
		unset($this->im);
		unset($this->height);
		unset($this->width);
		unset($this->error);
	}
	
	//destruct function
	//param : null
	//return : null
	function __destruct()
	{
		if(!$this->im) imagedestroy ($this->im);
		unset($this->height);
		unset($this->width);
		unset($this->error);
	}
	
	//load function
	//param : $url file url
	//return : true/false
	function load($url)
	{
		$this->im = imagecreatefromjpeg($url);
		
		if(!$this->im) { $this->error .= "load error\r\n"; return false; }
		
		$this->width = imagesx ($this->im);
		$this->height = imagesy ($this->im);
		
		return true;
	}
	
	//write function
	//param : $url file url 
	//return : true/false
	function write($url)
	{
		if(!$this -> im) { $this->error .= "load error\r\n" ; return false; }
		
		if(!imagejpeg($this->im , $url)) { $this->error .= "write error\r\n"; return false;}
		
		return true;
	}
	
	//merge function
	//param : $source source image identifier
	//param : $w position x 
	//param : $h position y 
	//param : $width target width
	//param : $height target height
	//param : $r_b , $g_b , $b_b
	//return : true/false
	function merge($source , $w = 0 , $h = 0 , $width = -1 , $height = -1 , $r_b = '' , $g_b = '' , $b_b = '')
	{
		if(!$source ->im) { $this->error .= "source image is null\r\n"; return false;}
		
		if($width == -1) $width = $source -> width;
		if($height == -1) $height = $source -> height;
		
		if($w+$width > $this->width) { $this->error .= "merge sizex is not right\r\n"; return false;}
		if($h+$height > $this->height) { $this->error .= "merge sizey is not right\r\n"; return false;}
		
		list($ans_width , $ans_height) = $source -> resize($width , $height , '' , '' , '' );
		
		if($r_b == '') return imagecopy ( $this->im, $source->im, $w+($width-$ans_width)/2, $h+($height-$ans_height)/2, ($width-$ans_width)/2, ($height-$ans_height)/2, $ans_width, $ans_height );
		
		imagealphablending($this->im,true);
		$tran_color= imagecolorallocate($this->im,$r_b,$g_b,$b_b);
		
		for ($j=0;$j<=$source->height-1;$j++)
		{
			for ($i=0;$i<=$source->width-1;$i++)
			{
				$rgb = imagecolorat($source->im,$i,$j);
				$rgb_array = imagecolorsforindex($source->im, $rgb); 
				$r = $rgb_array['red'];
				$g = $rgb_array['green'];
				$b = $rgb_array['blue'];
				
				$now_color=imagecolorallocate($this->im,$r,$g,$b);
				if ($now_color==$tran_color)
				{
					continue;
				}
				else
				{
					imagesetpixel($this->im,$w+$i,$h+$j,$now_color);
				}

			}
		}
	}
	
	//resize function
	//param : $width 
	//param : $height
	//param : $r , $g , $b
	//return : true/false
	function resize($width , $height , $r = '' , $g = '' , $b = '')
	{
		if(!$this ->im) { $this->error .= "source image is null\r\n"; return false;}
		
		$im = imagecreatetruecolor($width, $height);
		if($r == ''){$r=255;$g=255;$b=255;}
		$background_color = imagecolorallocate($im, $r, $g, $b);
		ImageFilledRectangle($im,0,0,$width,$height,$background_color); 

		if($width / $height > $this->width / $this->height )
		{
			$ret = imagecopyresized($im, $this->im, ($width - ($this->width*$height)/$this->height)/2, 0, 0, 0, ($this->width*$height)/$this->height, $height, $this->width, $this->height);
			$ans_width = ($this->width*$height)/$this->height;
			$ans_height = $height;
			if(!$ret ) { $this->error .= "resize error\r\n"; return array();}
		}
		else
		{
			$ret = imagecopyresized($im, $this->im, 0, ($height - ($this->height*$width)/$this->width)/2,0, 0,  $width, ($this->height*$width)/$this->width, $this->width, $this->height);
			$ans_width = $width;
			$ans_height = ($this->height*$width)/$this->width;
			if(!$ret ) { $this->error .= "resize error\r\n"; return array();}
		}
		imagedestroy($this->im);
		$this->im = $im;
		$this->width = $width;
		$this->height = $height;
		
		return array($ans_width , $ans_height);
	}
	
	function cut($width , $height)
	{
		if(!$this->im) { $this->error = 'source image is null'; return false;}
		$im = imagecreatetruecolor($width, $height);
		
		if($width / $height > $this->width / $this->height )
		{
			$ret = imagecopyresized($im, $this->im, 
			0, 0,
			0, ($this->height - ($height * $this -> width)/$width)/2,  
			$width, $height, 
			$this->width, ($height * $this -> width)/$width);
			
			if(!$ret ) { $this->error .= "resize error\r\n"; return false;}
			
		}
		else
		{
			$ret = imagecopyresized($im, $this->im, 
					0, 0, 
					($this->width - ($width * $this->height)/$height)/2, 0, 
					$width, $height, 
					($width * $this->height)/$height, $this->height);
					
			if(!$ret ) { $this->error .= "resize error\r\n"; return false;}
			
		}
		imagedestroy($this->im);
		$this->im = $im;
		$this->width = $width;
		$this->height = $height;
		
		return true;
	}
	
	//wartermark function
	//param : $str
	//param : $width
	//param : $height
	//param : $size
	//param : $angle
	//param : $w
	//param : $h 
	//param : $font
	//param : $r , $g , $b
	function watermark($str , $width , $height , $size , $angle , $w , $h , $font , $r , $g , $b)
	{
		$this->im = imagecreatetruecolor($width, $height);
		$background_color = imagecolorallocate($this->im, 255, 255, 255);
		ImageFilledRectangle($this->im,0,0,$width,$height,$background_color); 
		$grey = imagecolorallocate($this->im, $r, $g, $b);

		$ret = imagettftext($this->im, $size, $angle, $w, $h, $grey, $font, $str);
		if(!$ret) { $this->error = "watermark init error\r\n"; return false;}
		
		$this->width = imagesx ($this->im);
		$this->height = imagesy ($this->im);
		
		return true;
	}
	
	function watercloud($str , $width , $height , $font , $r , $g , $b)
	{
		$this->im = imagecreatetruecolor($width, $height);
		$background_color = imagecolorallocate($this->im, 255, 255, 255);
		ImageFilledRectangle($this->im,0,0,$width,$height,$background_color); 
		$grey = imagecolorallocate($this->im, $r, $g, $b);
		$this->width = imagesx ($this->im);
		$this->height = imagesy ($this->im);
		$str_array = explode("," , $str);
		$w = 0;
		foreach($str_array as $str)
		{
			$now_size = rand(16,26);
			$h = rand(30 , $height-10);
			list($s1 , $s2 , $now_w , $now_h , $s3 , $s4 , $s5 , $s6) = imagettfbbox ( $now_size, 0, $font, $str );
			if($w + $now_w > $width) break;
			$ret = imagettftext($this->im, $now_size, 0, $w, $h, $grey, $font, $str);
			
			$w  = $w + $now_w + rand(10 , 30);
		}
			
	}
	
	function rotate($degree , $r='' , $g='' , $b='')
	{
		if($b == '') {$r = 255;$b = 255; $g = 255;}
		if(!$this ->im) { $this->error .= "source image is null\r\n"; return false;}
		$background_color = imagecolorallocate($this->im, $r, $g, $b);
		$this->im = imagerotate($this->im, $degree, $background_color);
		$this->width = imagesx ($this->im);
		$this->height = imagesy ($this->im);
		
		return true;
	}
	
	function imagelinethick($image, $x1, $y1, $x2, $y2, $color, $thick = 1)
	{
		/* 下面两行只在线段直角相交时好使
		imagesetthickness($image, $thick);
		return imageline($image, $x1, $y1, $x2, $y2, $color);
		*/
		if ($thick == 1) {
			return imageline($image, $x1, $y1, $x2, $y2, $color);
		}
		$t = $thick / 2 - 0.5;
		if ($x1 == $x2 || $y1 == $y2) {
			return imagefilledrectangle($image, round(min($x1, $x2) - $t), round(min($y1, $y2) - $t), round(max($x1, $x2) + $t), round(max($y1, $y2) + $t), $color);
		}
		$k = ($y2 - $y1) / ($x2 - $x1); //y = kx + q
		$a = $t / sqrt(1 + pow($k, 2));
		$points = array(
			round($x1 - (1+$k)*$a), round($y1 + (1-$k)*$a),
			round($x1 - (1-$k)*$a), round($y1 - (1+$k)*$a),
			round($x2 + (1+$k)*$a), round($y2 - (1-$k)*$a),
			round($x2 + (1-$k)*$a), round($y2 + (1+$k)*$a),
		);
		imagefilledpolygon($image, $points, 4, $color);
		return imagepolygon($image, $points, 4, $color);
	}
	
	function lineto($x1 , $y1 , $x2 , $y2 , $color  , $thick = '5')
	{
		$this->imagelinethick($this->im , $x1 , $y1 , $x2 , $y2 , $color , $thick);
	}
};

function GetPic($headurl , $reurl , $backdir , $outdir , $font , $font2,$text1 , $text2 , $text3 , $text4 , $text5)
{	
	$bk = new Image();
	$bk->load($backdir);
	
	$wt1 = new Image();
	$wt2 = new Image();
	$wt3 = new Image();
	$wt4 = new Image();
	$wt5 = new Image();
	
	$hd1 = new Image();
	$hd2 = new Image();
	
	$hd1->load($headurl);
	$hd2->load($reurl);
	// $basedir = dirname(__FILE__);
	// $hd1->resize(100 , 100);
	// $hd1->write("$basedir\\1.jpg");
	
	$wt1->watermark($text1 ,  460 , 30 ,18 , 0 ,  0 ,25 , $font , 40 , 109 , 142);//最上方
	$wt2->watermark($text2 , 460 , 80 , 14 , 0 , 0 , 20 , $font2 , 0 , 0 , 0);//水瓶座的
	$wt3->watermark($text3 , 500 , 30 , 18 , 0 ,  0 , 25 ,$font , 40 , 109 , 142);
	$wt4->watermark($text4 , 500 , 80 , 14 , 0 ,  0 ,20 , $font2 , 0 , 0 , 0);//水瓶座的
	$wt5->watercloud($text5 , 460 , 100 , $font , 0 ,0 , 0);
	
	$bk->merge($wt1 ,  70 ,130 , -1 , -1 , 255 , 255 , 255);
	$bk->merge($wt2 ,  70,175 , -1 , -1 , 255 , 255 , 255);
	$bk->merge($wt3 ,  200,375 , -1 , -1 , 255 , 255 , 255);
	$bk->merge($wt4 ,  200, 420 ,-1 , -1 , 255 , 255 , 255);
	$bk->merge($wt5 ,  70,260 , -1 , -1 , 255 , 255 , 255);
	$bk->merge($hd1 , 530 , 110 , 200 , 200);
	$bk->merge($hd2 , 70 , 375 , 100 , 100);
	
	$bk->write($outdir);
}
// $basedir = dirname(__FILE__);
// GetPic('http://hdn.xnimg.cn/photos/hdn221/20110121/1155/h_large_PO8g_1e8b0001a4ed2f75.jpg',  'http://hdn.xnimg.cn/photos/hdn221/20110121/1155/h_large_PO8g_1e8b0001a4ed2f75.jpg' , "$basedir\\back.jpg" , "$basedir\\mabaochang.jpg" , "$basedir\\msyhbd.ttf" , 
		// "北京大学的@她小花被推荐了!",
		// "水瓶座的女孩子-家乡是重庆市的女孩子-家乡是重庆市水瓶座的女孩子的女孩子-家乡是重庆市水瓶座的女孩子\n在北京大学-心理系读书\n找@他小草家少我们认识吧",
		// "北京大学的@他小草推荐了ta~",
		// "他给@她小花的推荐信：\r\n好人啊水瓶座的女孩子-家乡是重庆市水瓶座的女孩子-家乡是重庆市\n牛人啊",
		// "靠谱,非常靠谱,人格担保的靠谱" , "$basedir\\msyh.ttf");

function Resize($inputurl , $outdir , $width = 200 , $height = 200)
{
	$now = new Image();
	$now -> load($inputurl);
	
	$now -> cut($width , $height);
	
	$now ->write($outdir);
	
	if($now ->error == '') return true;
	return false;
}

function LineTo($bg , $match_array , $out)
{
	$now = new Image();
	$now -> load($bg);
	foreach($match_array as $match_one)
	{
		$x = $match_one['x'];
		$y = $match_one['y'];
		$now->lineto($x*100 , 150 , $y*100 , 300 , 255*256*256);
	}
	$now->write($out);
}

$basedir = dirname(__FILE__);

//LineTo("$basedir/bg.jpg" , array(array('x'=>1 , 'y'=>3),array('x'=>2 , 'y'=>4)) , "$basedir/mabaochang.jpg");
